package com.slimbloody.server;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import java.util.List;

@RestController
@RequestMapping("/server")
@Slf4j
public class TestController {
  @Autowired
  private DiscoveryClient discoveryClient;

  private static final String grpcPort= "gRPC.port";

  @GetMapping("/clientInfo")
  public List<ServiceInstance> clientInfo() {
    System.out.println();
    List<ServiceInstance> instances = discoveryClient.getInstances("client-app");
    return instances;
  }

  @GetMapping("/serverInfo")
  public List<ServiceInstance> serverInfo() {
    System.out.println();
    List<ServiceInstance> instances = discoveryClient.getInstances("server-app");
    log.info("server port: {}", instances.get(0).getMetadata().get(grpcPort));
    return instances;
  }

  @GetMapping("/agentInfo")
  public List<ServiceInstance> agentInfo() {
    System.out.println();
    List<ServiceInstance> instances = discoveryClient.getInstances("tf-serving-agent");
    log.info("tf-serving-agent port: {}", instances.get(0).getMetadata().get(grpcPort));
    return instances;
  }
}
